home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / jdirecttalker example / jdirecttalker.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  2.6 KB  |  100 lines

  1. /**
  2.  * File:        MRJTalker.java
  3.  *
  4.  *    Contains:    Implementations of a simple Speech Manager class for Java
  5.  *                    using the MRJ JDirect Implementation.  Requires the Speech
  6.  *                    Manager extension to be present to run.
  7.  *
  8.  *    Version:    1.5
  9.  *
  10.  *    Technology:    System 7.6.1 or later
  11.  *    
  12.  *    Package:    Mac OS Runtime For Java™ SDK 2.0.1
  13.  *
  14.  *    Copyright:    © 1984-1998 by Apple Computer, Inc.
  15.  *                    All rights reserved.
  16.  *
  17.  *    Bugs?:        If you find a problem with this file, report them to mrj_bugs.
  18.  *                    Include the file and version information (from above)
  19.  *                    in the problem description and send to:
  20.  *                    Internet:    mrj_bugs@apple.com
  21.  */
  22.  
  23.  
  24. /**
  25.  * This class is a re-implementation of the NativeLibrary example, MRJTalker.
  26.  * It uses Apple's JDirect technology to access the speech library directly.
  27.  */
  28. import  com.apple.jdirect.SharedLibrary;
  29. import  com.apple.mrj.jdirect.JDirectLinker;
  30.  
  31. public interface MRJTalkerLibrary extends SharedLibrary {
  32.     static Object libraryInstance = JDirectLinker.loadLibrary("SpeechLib");
  33. }
  34.  
  35. public class MRJTalker implements MRJTalkerLibrary {    
  36.     /**
  37.      * We don't allow anyone to instantiate us
  38.      */    
  39.     protected MRJTalker() {
  40.     }
  41.     
  42.     /**
  43.      * Because the default speech channel is a system resource
  44.      * and we must limit access to it, we act as a "factory" object
  45.      * and only ever return one instance of it.
  46.      *
  47.      * Note that loadLibrary is case sensitive, and the name
  48.      * of the code fragment must match precisely - we use the
  49.      * CFM to resolve these names, as described in
  50.      * "Inside Macintosh: PowerPC System Software"
  51.      */
  52.     static MRJTalker theTalker = null;
  53.     
  54.     public static MRJTalker getTalker() {
  55.         if (theTalker == null) {
  56.             theTalker = new MRJTalker();
  57.         }
  58.         return theTalker;
  59.     }
  60.     
  61.     /**
  62.      * The method that does the talking is synchronized
  63.      * so that only one person can say something at a time.
  64.      */
  65.     public synchronized void speakString(String s) {
  66.         // turn the string into a pascal string array of bytes
  67.         byte b[] = toStr255(s);
  68.         
  69.         // call the native method
  70.         SpeakString(b);
  71.         
  72.         // block until the speech is done
  73.         while (SpeechBusy()) {
  74.             try { 
  75.                 Thread.currentThread().sleep(250);
  76.             } catch (InterruptedException e) {
  77.             }
  78.         }
  79.     }
  80.     
  81.     /** converts a Java string to a Pascal-style string. */
  82.     private static byte[] toStr255(String str) {
  83.         int length = str.length();
  84.         if (length > 255) length = 255;
  85.         byte result[] = new byte[length + 1];
  86.         result[0] = (byte)length;
  87.         str.getBytes(0, length, result, 1);
  88.         return result;
  89.     }
  90.     
  91.     /**
  92.      * The JDirect implementation
  93.      */
  94.  
  95.  
  96.     /** Declare the native methods that we're accessing */
  97.     private native static void SpeakString(byte b[]);
  98.     private native static boolean SpeechBusy();
  99. };
  100.